﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace CQFQW
{
    /// <summary>
    /// 获取数据
    /// </summary>
    public class MatchData
    {
        /// <summary>
        /// 起始字符串
        /// </summary>
        public string StartStr { get; set; }
        /// <summary>
        /// 结束字符串
        /// </summary>
        public string EndStr { get; set; }
        /// <summary>
        /// 查找源
        /// </summary>
        private string sourceData;

        public string SourceData
        {
            get { return sourceData; }
            set
            {
                if (value == null)
                    sourceData = "";
                else
                    sourceData = value;
            }
        }
        public const string MATCH_STR = "(.|\n)*?";

        public MatchData(string startStr, string endStr, string sourceData)
        {
            this.StartStr = startStr;
            this.EndStr = endStr;
            this.SourceData = sourceData;
        }

        /// <summary>
        /// 截取 起始和结束位置中间部分
        /// </summary>
        /// <param name="matchData"></param>
        /// <returns></returns>
        public string Match(RegexOptions options)
        {
            return Regex.Match(SourceData, this.StartStr + MatchData.MATCH_STR + this.EndStr, options).Value
                .TrimStart(this.StartStr.ToCharArray()).TrimEnd(this.EndStr.ToCharArray());
              
        }


        /// <summary>
        /// 截取 起始和结束位置中间部分 
        /// </summary>
        /// <param name="matchData"></param>
        /// <returns></returns>
        public Match[] Matches(RegexOptions options)
        {
            var s= Regex.Matches(SourceData, this.StartStr + this.EndStr,options);
            Match[] strResult = new Match[s.Count];
            s.CopyTo(strResult, 0);

            return strResult;
                
        }
    }
}
